﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using BBMS.DLL.Base;
using BBMS.FrontEnd.Constant;
using ExcelLibrary.SpreadSheet;

namespace BBMS.DLL.Forms.Catalog.NhatKyHT
{
    public partial class frmLogFilter : FrmFilterBase
    {
        public frmLogFilter()
        {
            InitializeComponent();            
        }


        public new static readonly string FunctionKey = "NhatKyHT";
        protected override string GetFunctionKey() { return "NhatKyHT"; }
        protected override BBMS.DLL.Base.ObjectBase GetObjectBase() { return new Log(); }

        protected override void GenColHeaders()
        {
            lvList.Clear();
            lvList.Columns.Add("Mã", 80);
            lvList.Columns.Add("Tên đăng nhập", 150);
            lvList.Columns.Add("Thao tác", 100);
            lvList.Columns.Add("Ngày", 100);
            lvList.Columns.Add("Nội dung", 100);
            lvList.Columns.Add("Địa chỉ IP", 150);
        }
        protected override ArrayList GenSampleData()
        {
            ArrayList ar = new ArrayList();
            ar.Add(new string[] { "STT", "Tên đăng nhập", "Thao tác", "Ngày", "Nội dung", "Địa chỉ IP" });
            Log logList = new Log();
            ArrayList arr = logList.GetAllObject(fParams);
            if (arr != null)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    Log l = (Log)arr[i];
                    ar.Add(new string[] { l.ID.ToString(), l.Username, l.Action.ToString(), l.LogDate.ToString(), l.Message, l.ComputerIP });
                }
            }
          //  ar.Add(new string[] { "QTP", "Quầy thực phẩm", "Food counter", "NTP" });
            // define col width
            //ar.Add(new int[] { 100, 300, 100, 300 });
            return ar;
        }

        protected override ArrayList GetImportedList(Worksheet sheet)
        {
            ArrayList arret = new ArrayList();
            for (int i = 1; i < sheet.Cells.Rows.Count; i++)
            {
                Log l = new Log();
                Cell cell = sheet.Cells[i, 0];
                l.ID = Convert.ToInt32(cell.Value.ToString());
                cell = sheet.Cells[i, 1];
                l.Username = cell.Value.ToString();
                cell = sheet.Cells[i, 2];
                if (cell.Value.ToString() != "") l.Action = Convert.ToInt16(cell.Value.ToString());
                cell = sheet.Cells[i, 3];
                if (cell.Value.ToString() != "") l.LogDate = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 4];
                l.Message = cell.Value.ToString();
                cell = sheet.Cells[i, 5];
                l.ComputerIP = cell.Value.ToString();
                arret.Add(l);

            }
            return arret;
        }

        protected override void UpdateFilterParams()
        {
            //if (txtMa.Text.Trim() != "" || cbLog.SelectedIndex != -1)
            //{
            //    fParams = new FilterParams();
            //    if (txtMa.Text.Trim() != "")
            //    {
            //        FilterCondition fcMa = new FilterCondition("Ma", "Ma", "%" + txtMa.Text.Trim() + "%", SQLOperator.LIKE);
            //        fParams.ArParams.Add(fcMa);
            //    }

            //    if (cbLog.SelectedIndex != 0)
            //    {
            //        FilterCondition fcLog = new FilterCondition("MaLog", "MaLog", ((KeyValuePair)cbLog.SelectedItem).Key, SQLOperator.EQUAL);
            //        fParams.ArParams.Add(fcLog);
            //    }
            //    fParams.CurrentPage = 0;
            //}
            //else
            //{
            //    MessageBox.Show(null, "Bạn phải nhập nội dung tìm kiếm", "Thông báo");
            //}
        }

        protected override FrmEditBase CreateEditForm(BBMS.DLL.Base.ObjectBase obj)
        {
            return new frmCounterUpdate(obj);
        }

        protected override void AddNewObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            

            ListViewItem it = new ListViewItem(((Log)obj).ID.ToString());
            it.SubItems.Add(((Log)obj).Username);
            String actionString = "";
            
            foreach (Action item in Enum.GetValues(typeof(Action)))
            {
                if (((int)item).ToString() == ((Log)obj).Action.ToString())
                {
                    actionString = StringEnum.GetStringValue(item);
                }
            }
            it.SubItems.Add(actionString);

            it.SubItems.Add(((Log)obj).LogDate.ToString());
            it.SubItems.Add(((Log)obj).Message);
            it.SubItems.Add(((Log)obj).ComputerIP);
            it.Tag = obj;
            lvList.Items.Add(it);
        }

        protected override void UpdateObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            foreach (ListViewItem it in lvList.Items)
            {
                if (((Log)it.Tag).ID == ((Log)obj).ID)
                {
                    it.SubItems[1].Text = ((Log)obj).Username;
                    String actionString = "";

                    foreach (Action item in Enum.GetValues(typeof(Action)))
                    {
                        if (((int)item).ToString() == ((Log)obj).Action.ToString())
                        {
                            actionString = StringEnum.GetStringValue(item);
                        }
                    }
                    it.SubItems[2].Text = actionString;
                    it.SubItems[3].Text = ((Log)obj).LogDate.ToString();
                    it.SubItems[4].Text = ((Log)obj).Message.ToString();
                    it.SubItems[5].Text = ((Log)obj).ComputerIP.ToString();
                    it.Tag = obj;
                    break;
                }
            }
        }
     
    }
}
